/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bootstrap.jsf.bootstrap.component.output;

import br.com.bootstrap.jsf.bootstrap.component.util.Utilities;
import java.io.IOException;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

/**
 *
 * @author alex
 */
@FacesComponent("markerWord")
public class MarkerWord extends UIOutput {

    private static final String COMPONENT_FAMILY = "br.com.bootstrap.jsf";
    private String id;
    private static final String CLASS_PREFIX = "marker-word";
    private String words;
    private String color = "#3a87ad";
    private String styleClass;
    private String style;

    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        setId(getClientId());
        if (getAttributes().containsKey("words")) {
            setWords(getAttributes().get("words").toString());
        }

        if (getAttributes().containsKey("color")) {
            setColor(getAttributes().get("color").toString());
        }

        if (getAttributes().containsKey("styleClass")) {
            setStyleClass(getAttributes().get("styleClass").toString());
        }

        if (getAttributes().containsKey("style")) {
            setStyle(getAttributes().get("style").toString());
        }



        ResponseWriter rw = context.getResponseWriter();

        rw.startElement("div", this);
        rw.writeAttribute("id", getId(), "id");
        rw.writeAttribute("class", getId() + "  " + getStyleClass(), "class");
        rw.writeAttribute("style", getStyle(), "style");
        rw.write(Utilities.markerWords(getValue().toString(), getWords(), getColor()));

    }

    @Override
    public void encodeEnd(FacesContext context) throws IOException {
        ResponseWriter rw = context.getResponseWriter();
        rw.endElement("div");
    }

    /**
     * @return the words
     */
    public String getWords() {
        return words;
    }

    /**
     * @param words the words to set
     */
    public void setWords(String words) {
        this.words = words;
    }

    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the styleClass
     */
    public String getStyleClass() {
        return styleClass;
    }

    /**
     * @param styleClass the styleClass to set
     */
    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    /**
     * @return the style
     */
    public String getStyle() {
        return style;
    }

    /**
     * @param style the style to set
     */
    public void setStyle(String style) {
        this.style = style;
    }



    @Override
    public String getFamily() {
        return COMPONENT_FAMILY;
    }
}
